<?php
/**
 * 财务操作类
 * @version $Id$
 */
class MoneyAction extends BaseAction {	
	protected $cw=array(),$vcw=array(),$money,$element;
	function _initialize() {
		parent::_initialize ();
		parent::_cache ( 1, 'role' );
		$this->assign ( 'userid', $this->userid );
		import ( 'Com.Money' );
		import ( 'Com.User' );
		import ( "ORG.Util.Input" );
		$this->user = new User ();
		$this->money = new Money ();
		$this->money_auth = M ( "UserMoneyAuth" );
		$money_info = $this->money->money_info ( $this->userid );
		$this->assign ( 'money_info', $money_info );
		$this->cw = array (
				'guo999',
				'edwin',
				'adminwei',
				'guojiacheng',
				'guan100221' 
		);
		$this->vcw= array(
				 'guo999','edwin','adminwei','guojiacheng','caiwusmw','guan100221','zhangruizhuan','jianglei','kefu_gaofang','yhf1979',
				'wufang',
		);
		// add by tb 暂时
		if (!in_array( $this->username, $this->vcw, true ) ) {
			$this->_ajm ( 300, '权限不足!', 0 );
			exit;
		}
		
// 		if (! (in_array ( '16', $this->roles ) || in_array ( '1', $this->roles ))) {
// 			$this->_ajm ( 300, '权限不足,只有财务组与超级管理员才能访问!', 0 );
// 		}
		$this->element = "ORDER_BAK,ADMIN_BV";
	}
	
	/*
	 * 财务操作权限
	 * */
	function cw(){
		$admid = $this->cw;
		// test by tb 数据库数据有误，暂时
		if (! in_array ( $this->username, $admid, true ) && $_SESSION ['groupid'] != 16) {
			$this->_ajm ( 300, '权限不足!', 0);
			exit;
		}
	}

	function index(){
		
		$dao = M("UserMoney");
		$where = array();
		if($_REQUEST['platform']){
			$where['platform'] = $_REQUEST['platform'];
		}
		if($_REQUEST['changtype']){
			$where['changtype'] = array('IN',$_REQUEST['changtype']);
		}
		if($_REQUEST['subject']){
			$where['subject'] = $_REQUEST['subject'];
		}
		$where=parent::create_where($where);
		$count = $dao->field('um.* , u.username')->join(' as um left join `user` as u on u.id = um.userid')->where($where)->count();
		$this->assign("totalCount",$count);
		$page=new Page($count,$this->listrows);
		$offset = $page -> firstRow;
		$length = $page -> listRows;
// 		$map = array();
// 		foreach ( $where as $key=>$value ) {
// 			$map[ 'u.'. $key ] = $value;
// 		}
		//unset($where);
		$list = $dao->field('u.username, um.*')->join(' as um left join `user` as u on u.id=um.userid')
				->where($where)->order($this->order)->limit($offset,$length)->select();
		//$list = $this->money->assets('','SMW01','','','ADMIN_ADD,TENPAY_ADD');echo 44;
		//print_r($list);
		//echo $dao->getLastSql();
		$this->assign('menu',$_REQUEST['menu']);
		$this->assign('where',$where);
		$this->assign('list',$list);
		$this->display();
	}

	
	/*
	 * 优惠券列表
	 * @author tb
	 * date 2014.2.8
	 * */
	function coupon(){
		$where = array();	
		if($_REQUEST['status']){
			$conditon['status'] = $_REQUEST['status'];
			if($_REQUEST['status'] == 3){
				$where['uc.status'] = 0;
			}else{
				$where['uc.status'] = $_REQUEST['status'];
			}
		}
		$starttime = $_REQUEST['stime'];
		$endtime = $_REQUEST['etime'];
		if($starttime)$conditon['starttime'] = ($starttime);
		if($endtime)$conditon['endtime'] = ($endtime);
		
		$where = parent::create_date(array('stime'=>strtotime($starttime),'etime'=>strtotime($endtime)),'uc.createtime',$where);		
		$where = parent::create_where($where);
		//获取产品优惠信息。
		$model = M('UserCoupon');
		$count = $model->join(' as uc left join user as u on uc.userid = u.id')->where($where)->count();
		$page=new Page($count,$this->listrows);
		$offset = $page -> firstRow;
		$length = $page -> listRows;
		
		$res = $model->field('uc.*,u.username')
		->join(' as uc left join user as u on uc.userid = u.id')
		->where($where)->order($this->order)->limit($offset,$length)->select();
		
		$sum = $model->where($where)->sum('value/100');
		$this->assign('sum',$sum);
		
		//	dump($res);echo $model->getLastSql();
		//$pro_coupon = $this->product_coupon($proid,'SALE');
		$this->assign("totalCount",$count);
		$this->assign("list",$res);
		$this->assign('conditon',$conditon);
		$this->display();
	}
	
	/*
	 * 删除优惠券
	 * @author tb
	 * @date 2014.2.08
	 * */
	function delcoupon(){
		$id = $_GET['id'];
		if(!$id){
			$this->_ajm(300,'选择错误');
			exit();
		}
		$model = M('UserCoupon');
		$reid = $model->delete($id);
		if($reid){
			$this->_ajm(200,'删除成功',1);
		}else{
			$this->_ajm(300,'删除失败',1);
		}
	}
	
	function addcoupon() {
		//判断权限
		$this->cw();
		
		$this->display ();
	}
	/**
	 * 显示充值留言
	 */
	function payinfo() {
		$map = array ();
		$status = $_REQUEST ['status'];
		$typeid = $_REQUEST ['typeid'];
		if ($status !== null && $status !== '') {
			$map ['status'] = $status;
		}
		if ($typeid !== null && $typeid !== '') {
			$map ['typeid'] = $typeid;
		}
		$sfield = $_REQUEST ['sfield'];
		if ($sfield == 'all') {
			$map = parent::create_where ( $map );
		} else {
			$map [$sfield] = $this->keywords;
		}
		$model = M ( 'UserBankPay' );
		$count = $model->where ( $map )->count ();
		$list = array ();
		if ($count > 0) {
			$p = new Page ( $count, $this->listrows );
			//$order = 'createtime desc ';
			$list = $model->where ( $map )->order ( $this->order )->limit ( $p->firstRow . ',' . $p->listRows )->select ();
		}
		$this->assign ( 'list', $list );
		$this->assign ( 'totalCount', $count );
		$userid = array ();
		$user = M ( 'User' );
		foreach ( $list as $v ) {
			$userid [] = $v ['userid'];
		}
		$userid = implode ( ',', $userid );
		$userdata = $user->field ( 'id,username' )->where ( "id IN ({$userid})" )->select ();
		$this->assign ( 'userdata', field2key ( $userdata, 'id' ) );
		$status = array (
				'0' => '未处理',
				'1' => '处理中',
				'2' => '已处理完' 
		);
		$type = array (
				'0' => array (
						'msg' => '待核查...',
						'title' => '待核查' 
				),
				'1' => array (
						'msg' => '核查属实，等待操作入账。',
						'title' => '属实' 
				),
				'2' => array (
						'msg' => '暂无到账记录，等待次日核查。',
						'title' => '次日核查' 
				),
				'3' => array (
						'msg' => '无到账记录，关闭。',
						'title' => '无到账' 
				),
				'4' => array (
						'msg' => '重复提交,关闭。',
						'title' => '重复提交' 
				),
				'5' => array (
						'msg' => '核查属实，已操作入账，关闭。',
						'title' => '已入账' 
				) 
		);
		$this->assign ( 'status', $status );
		$this->assign ( 'type', $type );
		$this->assign ( 'map', $map );
		$this->assign ( 'sfield', $sfield );
		$this->display ();
	} // end payinfo
	
	/**
	 * 调整用户余额
	 */
	function money() {
		//判断权限
		$this->cw();
		
		$user = $this->user->get_userinfo ( $_GET ['userid'], 'SMW01' );
		$user = array (
				'id' => $user ["user"] ['id'],
				'username' => $user ["user"] ['username'],
				'groupid' => $user ["role_user"] [0] ['role_id'] 
		);
		$model = M ( 'UserBankPay' );
		$bankpay = $model->field ( "id,round(moneyprice/100) as moneyprice,kahao,liushuisn,status" )->find ( $_GET ['id'] );
		$this->assign ( 'rlist', F ( 'Role' ) );
		$this->assign ( 'bankpay', $bankpay );
		$this->assign ( 'vo', $user );
		$this->display ();
	} // end money
	
	/**
	 * 调整用户资金
	 * 调整赠送帐户，记录赠送帐户信息
	 */
	function chumoney() {
		//判断权限
		$this->cw();
		
		$user = M ( "User" );
		if (! $user->autoCheckToken ( $_POST )) {
			$this->_ajm ( 300, "请不要重复操作", 1, 'Money/payinfo' );
			exit ();
		}
	
		$userid   = intval($_POST['userid']);
		$userinfo = $user->where()->find ($userid);
		$money    = M("UserMoneyAuth");
		$givedao  = M("UserGive");
		$givelogs = M("UserGiveLogs");
	
	
		$bzvu     = $_POST['bzvu']; // 备注
		$cvqishu  = $_POST['cvqishu']?intval($_POST['cvqishu']):0; // 分期数
		$give     = $_POST['give']?intval($_POST['give'])*100:0; // 增加赠送账户$_POST['amount']
		$shouqi = $_POST['shouqi'] ? intval($_POST['shouqi']) : 0;
	
		//首期金额不能大于，增加金额，或者小于0（可等于0）
		if($shouqi > $_POST ['amount'] || $shouqi<0){
			$this->_ajm(300,'首期金额填写有误!',0);
			exit();
		}
		//首期金额小于增加金额时，期数 不能小于或者等于0
		if($shouqi < $_POST ['amount'] && $cvqishu<=0){
			$this->_ajm(300,'剩余金额请填写分期!',0);
			exit();
		}
		
		$czmoney = $_POST['czmoney']; //用户实际重回金额
	
		if(($_POST['amount']<0||$give<0) && empty($bzvu)){
			$this->_ajm(300,'进行扣款操作时备注信息不能为空!',0);
			exit();
		}
		
		// 查找授权码
		$where = array ();
		$data ['money_auth'] = $_POST ['auth_money_auth'];
		$data ['money_passwd'] = $_POST ['auth_money_passwd'];
		$data ['statu'] = array (
				"eq",
				0 
		);
		$data ['uid'] = array (
				'eq',
				0 
		);
		$data ['userid'] = array (
				'eq',
				0 
		);
		$auth = $money->where ( $data )->find ();
		
		if (! empty ( $auth )) {
			$oedu = unserialize ( $auth ['oedu'] );
			// dump(array($oedu['amount'],$_POST['amount'],$oedu['give'],$_POST['give']));//die();
			// 检查授权额度与充值额度是否相符
			if ($oedu ['amount'] == intval ( $_POST ['amount'] ) * 100 && $oedu ['give'] == $give) {
				$money->startTrans ();
				// CV分期
				$fqamount = round ( $_POST ['amount'] - $shouqi ); //分期额度
				if ($cvqishu > 0 && $fqamount>0 ) {
					$amount = $this->cvfengqi ( $fqamount, $cvqishu, $userinfo, $givedao, $data ['money_auth'] );
				}
				// $gvo : 存储 user_give 数据 。 $vo : 存储 user_money_auth 数据。
				$gvo = $vo = array ();
				$vo ['statu'] = 1;
				$vo ['mtime'] = time ();
				$vo ['uid'] = $this->userid;
				$vo ['userid'] = $userid;
				$vo ['id'] = $auth ['id'];
				
				$uvo = array (); // 用户更新数据
				$uvo ['userid'] = $userid;
				
				if ($shouqi > 0) {
					$uvo ['CV'] = $shouqi;
				} else {
					if ($cvqishu > 0) {
						$uvo ['CV'] = 0;
					} else {
						$uvo ['CV'] = $_POST ['amount'];
					}
				}
				
				$qishu = intval ( $_POST ['qishu'] ); // 赠送分期数
				if ($qishu < 1 && $oedu ['give'] > 0) { // 没分期情况
					$gvo ['eachoedu'] = $gvo ['firstoedu'] = $oedu ['give'];
					$uvo ['BV'] = $oedu ['give']/100;
				} else { // 有分期的情况
					$gvo ['eachoedu'] = $gvo ['firstoedu'] = $oedu ['give'] / $qishu;
					$uvo ['BV'] = $gvo ['firstoedu']/100;
				}
				// $uvo['mtime']=$vo['mtime'];
				// $uvo['platform']="SMW01";
				// $uvo['groupid']=intval($_POST['groupid']);
				
				// 记录赠送帐户分期
				// 允许一个帐户拥有多个分期的每个分期的唯一码
				$only = short ( $vo ['mtime'] . '_' . $userid . '_' . $oedu ['give'] );
				$gvo ['only'] = $vo ['only'] = $only;
				$gvo ['startday'] = $gvo ['createtime'] = $vo ['mtime'];
				$gvo ['auth'] = $data ['money_auth'];
				$gvo ['qishu'] = $qishu;
				$gvo ['notoverqishu'] = $qishu;
				$endmon = date ( 'n' ) + $gvo ['notoverqishu'];
				$gvo ['endday'] = mktime ( 0, 0, 0, $endmon, date ( 'd' ) + 1, date ( 'Y' ) );
				$gvo ['status'] = $gvo ['notoverqishu'] > 1 ? '1' : '0';
				$gvo ['isover'] = $gvo ['notoverqishu'] > 1 ? '0' : '1';
				;
				$gvo ['userid'] = $userid;
				$gvo ['countoedu'] = $oedu ['give'];
				$gvo ['overoedu'] = 0;
				
				$gvo ['overqishu'] = 0;
				$gvo ['updatetime'] = $gvo ['createtime'];
				$gvo ['notoveroedu'] = $gvo ['countoedu'];
				$gvo ['moneytype'] = 'BV';
				// 写入分期综合信息
				// 分期的总额度为0时不写入分期表
				if ($gvo ['countoedu'] && $qishu > 0) {
					$givedao->add ( $gvo );
				}
				
				$vo ['content'] = $auth ['content'] . ':::qishu:' . $gvo ['qishu'] . '|eachoedu:' . $gvo ['eachoedu'];
				if (! empty ( $amount )) {
					$vo ['content'] .= ':::cvqishu:' . $cvqishu . '|cveachoedu:' . $amount ['eachoedu'];
				}
				// 充值CV 操作
				if ($uvo ['CV']) {
					$urs = $nowvalue = $this->money->recharge ( $userid, 'CV', $uvo ['CV'], array (
							'action_userid' => $this->userid,
							'cip' => get_client_ip (),
							'bzvu' => '转账充值',
							'changtype' => 'ADMIN_ADD' 
					) );
				}else{
					//首期 为 0
					$urs = true;
				}
				// 充值BV 操作
				if($qishu < 1){
					if ($uvo ['BV']) {
						$urs = $nowvalue = $this->money->recharge ( $userid, 'BV', $uvo ['BV'], array (
								'action_userid' => $this->userid,
								'cip' => get_client_ip (),
								'bzvu' => '转账充值',
								'changtype' => 'ADMIN_ADD' 
						) );
					}
				}
				// 修改授权码信息
				$rs = $money->save ( $vo );
				
				// 发送短信
				if ($rs && $urs) {
					
					//更改状态	 *****				
					$id = intval ( $_REQUEST ['id'] );
					$data = array ();
					$dao = M ( "UserBankPay" );
					$data = $dao->find ( $id );
						
					if ($data) {
						$content = unserialize ( $data ['reply_content'] );
						$content [] = array (
								'aid' => 11786,
								'time' => time (),
								'msg' => '核查属实，已操作入账，关闭。' . '<br>' 
						);
						$vo = array ();
						$vo ['reply_content'] = serialize ( $content );
						$vo ['typeid'] = 5;
						$vo ['status'] = 2;
						$vo ['id'] = $data ['id'];
						$res = $dao->save ( $vo );
						if ($res === false) {
							$this->_ajm ( 300, '操作失败，请重试', 0 );
						} 
					} else {
						$this->_ajm ( 300, '操作的支付留言不存在', 0 );
					}					
					// end 更改状态	 *****
					
					// 账户余额
					$uprice = array ();
					$uprice ['CV'] = round ( $this->money->money_info ( $userid, 'SMW01', 'CV' ) ) / 100;
					$uprice ['BV'] = round ( $this->money->money_info ( $userid, 'SMW01', 'BV' ) ) / 100;
					
					// 构建短信信息
					$str = '您在' . date ( 'm月d日H时i分' ) . '充值' . $czmoney . 'CV 赠送 ' . round ( $_POST ['amount'] - $czmoney ) . 'CV 成功！已到账' . $shouqi . ' CV 。';
					// 写入分期信息
					if ($cvqishu > 0) {
						$str .= '余下' . round ( $_POST ['amount'] - $shouqi ) . ' CV 将分' . $cvqishu . '个月到达您的账号！';
					}
					$str .= '您 CV余额为：' . round ( $uprice ['CV'] + $shouqi );
					$str .= '本次充值赠送：' . $oedu ['give'] . 'BV';
					$msg = iconv ( "UTF-8", "GB2312//TRANSLIT", $str );
					$dxinf = sendMobiMsg ( $userinfo ['user'] ['mobile'], $msg );
					
					$this->_ajm ( 200, '操作成功!', 0, U ( 'Money/payinfo' ) );
				} else {
					$this->_ajm ( 300, '操作失败，请重试!', 0 );
				}
			} else {
				$this->_ajm ( 300, '“授权码授权金额”和“预付款金额”不一致！', 0 );
			}
		} else {
			$this->_ajm ( 300, '检查授权码或者授权密码错误，或授权码已用！', 0 );
		}
	}
	
	/**
	 * 调整用户资金
	 * 调整赠送帐户，记录赠送帐户信息
	 */
	function chumoney_bak() {
		$admid = $this->cw;
		// test by tb 数据库数据有误，暂时
		if (! in_array ( $this->username, $admid, true ) && $_SESSION ['groupid'] != 16) {
			$this->_ajm ( 300, '权限不足!', 0 );
		}
		
		$user = M ( "User" );
		if (! $user->autoCheckToken ( $_POST )) {
			$this->_ajm ( 300, "请不要重复操作", 1, 'Money/payinfo' );
			exit ();
		}
		
		$userid = intval ( $_POST ['userid'] );
		$userinfo = $user->where ()->find ( $userid );
		$money = M ( "UserMoneyAuth" );
		$givedao = M ( "UserGive" );
		$givelogs = M ( "UserGiveLogs" );
		
		$bzvu = $_POST ['bzvu']; // 备注
		$cvqishu = $_POST ['cvqishu'] ? intval ( $_POST ['cvqishu'] ) : 0; // 分期数
		$give = $_POST ['give'] ? $_POST ['give'] : 0; // 增加赠送账户$_POST['amount']
		$shouqi = $_POST ['shouqi'] ? intval ( $_POST ['shouqi'] ) : 0;
		$czmoney = $_POST ['czmoney']; // 用户实际重回金额
		
		if (($_POST ['amount'] < 0 || $give < 0) && empty ( $bzvu )) {
			$this->_ajm ( 300, '进行扣款操作时备注信息不能为空!', 0 );
			exit ();
		}
		
		$data = array ();
		$data ['money_auth'] = $_POST ['auth_money_auth'];
		$data ['money_passwd'] = $_POST ['auth_money_passwd'];
		$data ['statu'] = array (
				"eq",
				0 
		);
		$data ['uid'] = array (
				'eq',
				0 
		);
		$data ['userid'] = array (
				'eq',
				0 
		);
		$auth = $money->where ( $data )->find ();
		if (! empty ( $auth )) {
			$oedu = unserialize ( $auth ['oedu'] );
			// dump(array($oedu['amount'],$_POST['amount'],$oedu['give'],$_POST['give']));//die();
			// 检查授权额度与充值额度是否相符
			if ($oedu ['amount'] == intval ( $_POST ['amount'] ) * 100 && $oedu ['give'] == $give) {
				$money->startTrans ();
				// CV分期
				if ($cvqishu > 0) {
					$amount = $this->cvfengqi ( round ( $_POST ['amount'] - $shouqi ), $cvqishu, $userinfo, $givedao, $data ['money_auth'] );
				}
				$glog = $gvo = $vo = array ();
				$vo ['statu'] = 1;
				$vo ['mtime'] = time ();
				$vo ['uid'] = $this->userid;
				$vo ['userid'] = $userid;
				$vo ['id'] = $auth ['id'];
				$uprice = array ();
				$userprice = M ( 'user_money' )->where ( array (
						'userid' => $userid 
				) )->select ();
				if ($userprice) {
					foreach ( $userprice as $valule ) {
						if ($valule ['subject'] == 'CV')
							$uprice ['CV'] = $valule ['value'];
						if ($valule ['subject'] == 'BV')
							$uprice ['BV'] = $valule ['value'];
						if ($valule ['subject'] == 'PV')
							$uprice ['PV'] = $valule ['value'];
						if ($valule ['subject'] == 'MV')
							$uprice ['MV'] = $valule ['value'];
					}
				}
				$uvo = array (); // 用户更新数据
				$uvo ['userid'] = $userid;
				
				if ($shouqi > 0) {
					$uvo ['CV'] = $shouqi;
				} else {
					if ($cvqishu > 0) {
						$uvo ['CV'] = 0;
					} else {
						$uvo ['CV'] = $_POST ['amount'];
					}
				}
				
				$glog ['oldoedu'] = $uprice ['BV']; // 之前余额
				$qishu = intval ( $_POST ['qishu'] ); // 赠送分期数
				if ($qishu < 1 && $oedu ['give'] > 0) { // 没分期情况
					$gvo ['eachoedu'] = $gvo ['firstoedu'] = $oedu ['give'];
					$uvo ['BV'] = $oedu ['give'];
				} else { // 有分期的情况
					$gvo ['eachoedu'] = $gvo ['firstoedu'] = $oedu ['give'] / $qishu;
					$uvo ['BV'] = $gvo ['firstoedu'];
				}
				$uvo ['mtime'] = $vo ['mtime'];
				$uvo ['platform'] = "SMW01";
				$uvo ['groupid'] = intval ( $_POST ['groupid'] );
				
				// 记录赠送帐户分期
				// 允许一个帐户拥有多个分期的每个分期的唯一码
				$only = short ( $vo ['mtime'] . '_' . $userid . '_' . $oedu ['give'] );
				$glog ['only'] = $gvo ['only'] = $vo ['only'] = $only;
				$gvo ['startday'] = $glog ['createtime'] = $gvo ['createtime'] = $vo ['mtime'];
				$gvo ['auth'] = $data ['money_auth'];
				$gvo ['qishu'] = $qishu;
				$gvo ['notoverqishu'] = $qishu - 1;
				$endmon = date ( 'n' ) + $gvo ['notoverqishu'];
				$gvo ['endday'] = mktime ( 0, 0, 0, $endmon, date ( 'd' ) + 1, date ( 'Y' ) );
				$gvo ['status'] = $gvo ['notoverqishu'] > 1 ? '1' : '0';
				$gvo ['isover'] = $gvo ['notoverqishu'] > 1 ? '0' : '1';
				;
				$glog ['userid'] = $gvo ['userid'] = $userid;
				$gvo ['countoedu'] = $oedu ['give'];
				$glog ['addoedu'] = $gvo ['overoedu'] = $gvo ['firstoedu'];
				
				$gvo ['overqishu'] = 1;
				$gvo ['updatetime'] = $gvo ['createtime'];
				$gvo ['notoveroedu'] = $gvo ['countoedu'] - $gvo ['overoedu'];
				$gvo ['moneytype'] = 'BV';
				// 写入分期综合信息
				// 分期的总额度为0时不写入分期表
				if ($gvo ['countoedu']) {
					$givedao->add ( $gvo );
				}
				$glog ['mon'] = date ( 'm', $gvo ['createtime'] );
				$glog ['nowoedu'] = $uvo ['BV'];
				$glog ['createtime'] = time ();
				// 写入到账日志
				// 分期的总额度为0时不写入分期到账日志表
				if ($gvo ['countoedu']) {
					$givelogs->add ( $glog );
				}
				$vo ['content'] = $auth ['content'] . ':::qishu:' . $gvo ['qishu'] . '|eachoedu:' . $gvo ['eachoedu'];
				if (! empty ( $amount )) {
					$vo ['content'] .= ':::cvqishu:' . $cvqishu . '|cveachoedu:' . $amount ['eachoedu'];
				}
				if ($uvo ['CV']) {
					
					$urs = $nowvalue = $this->money->recharge ( $userid, 'CV', $uvo ['CV'], array (
							'action_userid' => $this->userid,
							'cip' => get_client_ip (),
							'bzvu' => '转账充值',
							'changtype' => 'ADMIN_ADD' 
					) );
				}
				if ($uvo ['BV']) {
					$urs = $nowvalue = $this->money->recharge ( $userid, 'BV', $uvo ['BV'], array (
							'action_userid' => $this->userid,
							'cip' => get_client_ip (),
							'bzvu' => '转账充值',
							'changtype' => 'ADMIN_ADD' 
					) );
				}
				
				$rs = $money->save ( $vo );
				// 发送短信
				if ($rs && $urs) {
					$str = '您在' . date ( 'm月d日H时i分' ) . '充值' . $czmoney . 'CV 赠送 ' . round ( $_POST ['amount'] - $czmoney ) . 'CV 成功！已到账' . $shouqi . ' CV 。';
					// 写入分期信息
					if ($cvqishu > 0) {
						$str .= '余下' . round ( $_POST ['amount'] - $shouqi ) . ' CV 将分' . $cvqishu . '个月到达您的账号！';
					}
					$str .= '您 CV余额为：' . round ( $uprice ['CV'] + $shouqi );
					$str .= '本次充值赠送：' . $oedu ['give'] . 'BV';
					$msg = iconv ( "UTF-8", "GB2312//TRANSLIT", $str );
					// $dxinf=sendMobiMsg($userinfo['user']['mobile'],$msg);
					$dxinf = sendMobiMsg ( '18680345046', $msg );
					
					// $this->_wuserassets($oedu['amount'],$gvo['firstoedu'],$userid,$data['money_auth'],'ADMIN_ADD',0,0,0,$bzvu);
					
					$this->_ajm ( 200, '操作成功!', 0, U ( 'Money/payinfo' ) );
				} else {
					$this->_ajm ( 300, '操作失败，请重试!', 0 );
				}
			} else {
				$this->_ajm ( 300, '“授权码授权金额”和“预付款金额”不一致！', 0 );
			}
		} else {
			$this->_ajm ( 300, '检查授权码或者授权密码错误，或授权码已用！', 0 );
		}
	}
	
	
	/**
	 * 处理充值留言
	 */
	function pay() {
		$this->cw();
		$type = array (
				'0' => array (
						'msg' => '待核查...',
						'title' => '待核查' 
				),
				'1' => array (
						'msg' => '核查属实，等待操作入账。',
						'title' => '属实' 
				),
				'2' => array (
						'msg' => '暂无到账记录，等待次日核查。',
						'title' => '次日核查' 
				),
				'3' => array (
						'msg' => '无到账记录，关闭。',
						'title' => '无到账' 
				),
				'4' => array (
						'msg' => '重复提交,关闭。',
						'title' => '重复提交' 
				),
				/*'5' => array (
						'msg' => '核查属实，已操作入账，关闭。',
						'title' => '已入账' 
				) */
		);
		
		$id = intval ( $_REQUEST ['id'] );
		$data = array ();
		$dao = M ( "UserBankPay" );
		$data = $dao->find ( $id );
		
		if ($_POST ['act'] == 'submit') {
			$typeid = intval ( $_POST ['type'] );
			$status = $typeid == 0 ? '0' : ($typeid == 1 || $typeid == 2) ? '1' : '2';
			$msg = Input::deleteHtmlTags ( $_POST ['msg'] );
			
			
			if ($data) {
				$content = unserialize ( $data ['reply_content'] );
				$content [] = array (
						'aid' => $this->userid,
						'time' => time (),
						'msg' => $type [$typeid] ['msg'] . '<br>' . $msg 
				);
				$vo = array ();
				$vo ['reply_content'] = serialize ( $content );
				$vo ['typeid'] = $typeid;
				$vo ['status'] = $status;
				$vo ['id'] = $data ['id'];
				$res = $dao->save ( $vo );
				if ($res === false) {
					$this->_ajm ( 300, '操作失败，请重试', 0 );
				} else {
					$this->_ajm ( 200, '数据修改成功！', 0, U ( 'Money/payinfo' ) );
				}
			} else {
				$this->_ajm ( 300, '操作的支付留言不存在', 0 );
			}
		} else {
			$status = array (
					'0' => '未处理',
					'1' => '处理中',
					'2' => '已处理完' 
			);
			$this->assign('typeid',$data['typeid']);
			$this->assign ( 'status', $status );
			$this->assign ( 'type', $type );
			$this->display ();
		}
	}
	
	/**
	 * 授权码
	 */
	function authnum() {
		$map = array ();
		$status = $_REQUEST ['status'];
		if ($status === '0' || $status === '1') {
			$map ['statu'] = $status;
		}		
		$map = parent::create_where ( $map );
		
		$this->autogive ();
		$this->autocv ();
		$model = M ( 'UserMoneyAuth' );
		$count = $model->where ( $map )->count ();
		$list = array ();
		if ($count > 0) {
			$p = new Page ( $count, $this->listrows );
			$list = $model->where ( $map )->order ( $this->order )->limit ( $p->firstRow . ',' . $p->listRows )->select ();
		}
		$this->assign ( 'list', $list );
		$this->assign ( 'totalCount', $count );
		$this->assign ( 'map', $map );
		$this->assign ( 'sfied', $sfield );
		$this->display ();
	}
	
	/**
	 * 查看授权码
	 */
	function authview() {
		$id = intval ( $_GET ['id'] );
		$dao = M ( "UserMoneyAuth" );
		$vo = $dao->find ( $id );
		$this->assign ( 'vo', $vo );
		$this->display ();
	}
	
	/**
	 * 作废授权码
	 */
	function statu() {
		//判断权限
		$this->cw();
		
		$id = intval ( $_GET ['id'] );
		$dao = M ( "UserMoneyAuth" );
		$vo = $dao->find ( $id );
		if ($vo ['statu'] != 0) {
			$this->_ajm ( 300, '只能作废未使用的授权', 0 );
		} elseif ($vo ['statu'] == 0) {
			$data = array ();
			$data ['statu'] = '3';
			$rs = $dao->where ( "id={$vo['id']} AND statu=0" )->save ( $data );
			if ($rs) {
				$this->_ajm ( 200, '操作成功!', 1, U ( 'Money/authnum' ) );
			} else {
				$this->_ajm ( 300, '操作失败，请重试!', 0 );
			}
		}
	}
	
	/**
	 * 生产授权号与授权密码
	 */
	function cmoneyauth() {
		//判断权限
		$this->cw();
		
		if(!empty($_POST['amount'])||!empty($_POST['give'])){
			$amount = intval($_POST['amount'])*100; // 金额转成分
			$give = intval($_POST['give'])*100; //赠送BV
			$oedu=serialize(array('amount'=>$amount,'give'=>$give));
			$authstr='Admin:'.$this->userid.'|Amount:'.$amount.'|Give:'.$_POST['give'];
			$time=time();
			$money_auth=short($authstr.$time);
			$money_passwd=short(md5($money_auth.$time));
			$dao=M("UserMoneyAuth");
			if (!$dao->autoCheckToken($_POST)){
				$this->_ajm(300,'请不要重复操作',0);
			}
			$data = array ();
			$data ['ctime'] = $time;
			$data ['oedu'] = $oedu;
			$data ['content'] = $authstr;
			$data ['mtime'] = 0;
			$data ['money_auth'] = $money_auth;
			$data ['money_passwd'] = $money_passwd;
			$data ['statu'] = 0;
			$data ['aid'] = $this->userid;
			$data ['uid'] = 0;
			$data ['userid'] = 0;
			$data ['cip'] = get_client_ip ( 1 );
			$auth_id = $dao->add ( $data );
			if ($auth_id) {
				/*
				 * 还需要写入事件日志 logs 表
				 */
				$this->_ajm ( 200, '增加成功！', 0, U ( 'Money/authnum' ) );
			} else {
				/*
				 * 还需要写入事件日志 logs 表
				 */
				$this->_ajm ( 300, '操作错误，请重试', 0 );
			}
		} else {
			C ( 'TOKEN_ON', true );
			$this->display ();
		}
	}
	
	/**
	 * 给CV记录分期
	 *
	 * @param float $amount
	 *        	总充值CV
	 * @param int $cvqishu
	 *        	CV分期的期数
	 * @param int $userid        	
	 * @param M $gdao        	
	 * @param M $glogs        	
	 * @return array
	 */
	function cvfengqi($amount, $cvqishu, $userinfo, $gdao, $auth) {
		$admid = $this->cw;
		if (! in_array ( $this->username, $admid, true ) && $_SESSION ['groupid'] != 16) {
			$this->_ajm ( 300, '权限不足!', 0 );
		}
		$userid = $userinfo ['id'];
		$time = time ();
		$gvo = array ();
		$eachoedu = intval ( $amount * 100 / $cvqishu );
		$only = short ( $time . '_' . $userid . '_' . $amount );
		$gvo ['userid'] = $userid;
		$gvo ['countoedu'] = $amount*100; // 总额度
		$gvo ['startday'] = $gvo ['createtime'] = time (); // 开始时间 创建时间
		$gvo ['qishu'] = $cvqishu; // 总期数
		$gvo ['eachoedu'] = $eachoedu; // 每期到账
		$gvo ['overoedu'] = 0; // 已完成返回0
		$gvo ['firstoedu'] = $eachoedu; // 第一期额度
		$gvo ['notoveroedu'] = $gvo ['countoedu']; // 未返还额度
		$gvo ['notoverqishu'] = $cvqishu; // 未完成的期数
		$endmon = date ( 'n' ) + $gvo ['notoverqishu'];
		$gvo ['endday'] = mktime ( 0, 0, 0, $endmon, date ( 'd' ) + 1, date ( 'Y' ) ); // 结束时间
		$gvo ['only'] = $only;
		$gvo ['auth'] = $auth;
		$gvo ['status'] = $gvo ['notoverqishu'] > 1 ? '1' : '0';
		$gvo ['isover'] = $gvo ['notoverqishu'] > 1 ? '0' : '1';
		;
		$gvo ['overqishu'] = 0; // 已完成期数
		$gvo ['updatetime'] = $gvo ['createtime'];
		$gvo ['moneytype'] = 'CV';
		$res = $gdao->add ( $gvo );
		if ($res) {
			$res = array (
					"only" => $only,
					"eachoedu" => $eachoedu 
			);
			return $res;
		} else {
			$this->_ajm ( 300, '分期操作失败！' );
			exit ();
		}
	}
	
	/**
	 * 自动赠送分期到账的BV
	 */
	protected function autogive() {
		// 查询更新时间距离现在超过30天的记录
		$baktime = time () - 30 * 24 * 60 * 60;
		$give = M ( "UserGive" );
		$givelogs = M ( "UserGiveLogs" );
		$user = M ( "User" );
		$where = array ();
		$where ['updatetime'] = array (
				'lt',
				$baktime 
		);
		$where ['qishu'] = array (
				'gt',
				0 
		);
		// $where['overqishu']=array('lt','`qishu`');
		$where ['notoverqishu'] = array (
				'gt',
				'0' 
		);
		$where ['notoveroedu'] = array (
				'gt',
				'0' 
		);
		$where ['status'] = array (
				'eq',
				'1' 
		);
		$where ['isover'] = array (
				'eq',
				'0' 
		);
		$where ['moneytype'] = array (
				'eq',
				'BV' 
		);
		$list = array ();
		$list = $give->where ( $where )->limit ( '0,30' )->select ();
		if ($list) {
			foreach ( $list as $key => $v ) {
				if (! ($v ['overqishu'] < $v ['qishu'])) {
					continue;
				}
				// dump($v);
				$time = time ();
				$gdata = array ();
				$maps = array ();
				$udata = array ();
				$maps ['only'] = $v ['only'];
				$maps ['mon'] = date ( 'm' );
				$maps ['userid'] = array (
						'eq',
						$v ['userid'] 
				);
				if ($givelogs->where ( $maps )->find ()) {
					continue;
				} else {
					$maps ['addoedu'] = $v ['eachoedu'];
					$uinfo = $user->field ( 'id,give' )->where ( "id={$v['userid']}" )->find ();
					if (empty ( $uinfo )) {
						continue;
					}
					$maps ['userid'] = $v ['userid'];
					$maps ['oldoedu'] = $uinfo ['give'];
					$maps ['nowoedu'] = $uinfo ['give'] + $v ['eachoedu'];
					$maps ['createtime'] = $time;
					$maps ['only'] = $v ['only'];
					
					$udata ['updatetime'] = $time;
					$udata ['give'] = $maps ['nowoedu'];
					
					$gdata ['id'] = $v ['id'];
					$gdata ['userid'] = $v ['userid'];
					$gdata ['updatetime'] = $time;
					$gdata ['overqishu'] = $v ['overqishu'] + 1;
					$gdata ['notoverqishu'] = $v ['notoverqishu'] - 1;
					$gdata ['overoedu'] = $v ['overoedu'] + $v ['eachoedu'];
					$gdata ['notoveroedu'] = $v ['notoveroedu'] - $v ['eachoedu'];
					$gdata ['isover'] = $gdata ['overqishu'] == $v ['qishu'] ? 1 : 0;
					
					$givelogs->add ( $maps );
					$give->save ( $gdata );
					$user->where ( 'id=' . $v ['userid'] )->save ( $udata );
					$this->_wuserassets ( 0, $v ['eachoedu'], $v ['userid'], $v ['auth'] );
				}
			}
		}
	}
	
	/**
	 * 自动赠送分期到账的CV
	 */
	protected function autocv() {
		// 查询更新时间距离现在超过30天的记录
		$baktime = time () - 30 * 24 * 60 * 60;
		$give = M ( "UserGive" );
		$givelogs = M ( "UserGiveLogs" );
		$user = M ( "User" );
		$where = array ();
		$where ['updatetime'] = array (
				'lt',
				$baktime 
		);
		$where ['qishu'] = array (
				'gt',
				0 
		);
		// $where['overqishu']=array('lt','`qishu`');
		$where ['notoverqishu'] = array (
				'gt',
				'0' 
		);
		$where ['notoveroedu'] = array (
				'gt',
				'0' 
		);
		$where ['status'] = array (
				'eq',
				'1' 
		);
		$where ['isover'] = array (
				'eq',
				'0' 
		);
		$where ['moneytype'] = array (
				'eq',
				'CV' 
		);
		$list = array ();
		$list = $give->where ( $where )->limit ( '0,30' )->select ();
		if ($list) {
			foreach ( $list as $key => $v ) {
				if (! ($v ['overqishu'] < $v ['qishu'])) {
					continue;
				}
				// dump($v);
				$time = time ();
				$gdata = array ();
				$maps = array ();
				$udata = array ();
				$maps ['only'] = $v ['only'];
				$maps ['mon'] = date ( 'm' );
				$maps ['userid'] = array (
						'eq',
						$v ['userid'] 
				);
				if ($givelogs->where ( $maps )->find ()) {
					continue;
				} else {
					$maps ['addoedu'] = $v ['eachoedu'];
					$uinfo = $user->field ( 'id,amount' )->where ( "id={$v['userid']}" )->find ();
					if (empty ( $uinfo )) {
						continue;
					}
					$maps ['userid'] = $v ['userid'];
					$maps ['oldoedu'] = $uinfo ['amount'];
					$maps ['nowoedu'] = $uinfo ['amount'] + $v ['eachoedu'];
					$maps ['createtime'] = $time;
					$maps ['only'] = $v ['only'];
					
					$udata ['updatetime'] = $time;
					$udata ['amount'] = $maps ['nowoedu'];
					
					$gdata ['id'] = $v ['id'];
					$gdata ['userid'] = $v ['userid'];
					$gdata ['updatetime'] = $time;
					$gdata ['overqishu'] = $v ['overqishu'] + 1;
					$gdata ['notoverqishu'] = $v ['notoverqishu'] - 1;
					$gdata ['overoedu'] = $v ['overoedu'] + $v ['eachoedu'];
					$gdata ['notoveroedu'] = $v ['notoveroedu'] - $v ['eachoedu'];
					$gdata ['isover'] = $gdata ['overqishu'] == $v ['qishu'] ? 1 : 0;
					
					$givelogs->add ( $maps );
					$give->save ( $gdata );
					$user->where ( 'id=' . $v ['userid'] )->save ( $udata );
					$this->_wuserassets ( $v ['eachoedu'], 0, $v ['userid'], $v ['auth'] );
				}
			}
		}
	}
	
	/*
	 * 用户资金充值明细 @param 预付款变动 $yufu @param 赠送帐户变动 $yufuzs @param 关联用户ID $userid @param 操作授权码 $money_auth @return boolean
	 */
	 /* 用户资金充值明细
	 * @param 预付款变动  $yufu
	 * @param 赠送帐户变动  $yufuzs
	 * @param 关联用户ID $userid
	 * @param 操作授权码  $money_auth
	 * @return boolean
	 */
	protected function _wuserassets($yufu = 0, $yufuzs = 0, $userid = 0, $money_auth = '', $changtype = 'ADMIN_ADD', $ordersn = '0', $pv = 0, $mv = 0, $bzvu = '') {
		if (empty ( $userid )) {
			return false;
		} else {
			$dao = M ( "UserAssets" );
			$user = M ( "User" );
			$userinfo = $user->field ( 'id,amount,give,point,mv' )->where ( "id={$userid}" )->find ();
			$uprice = array ();
			$userprice = M ( 'user_money' )->where ( array (
					'userid' => $userid 
			) )->select ();
			if ($userprice)
				foreach ( $userprice as $valule ) {
					if ($valule ['subject'] == 'CV')
						$uprice ['CV'] = $valule ['value'];
					if ($valule ['subject'] == 'BV')
						$uprice ['BV'] = $valule ['value'];
					if ($valule ['subject'] == 'PV')
						$uprice ['PV'] = $valule ['value'];
					if ($valule ['subject'] == 'MV')
						$uprice ['MV'] = $valule ['value'];
				}
			if ($uprice) {
				$data = array ();
				$data ['money_auth'] = $money_auth;
				$data ['changtype'] = $changtype;
				$data ['changmoney'] = $yufu;
				$data ['changmv'] = $mv;
				$data ['changzs'] = $yufuzs;
				$data ['money'] = $uprice ['CV'];
				$data ['zsmoney'] = $uprice ['BV'];
				$data ['ctime'] = time ();
				$data ['cip'] = get_client_ip ( 1 );
				$data ['changjf'] = $pv;
				$data ['changyh'] = 0;
				$data ['yhsn'] = 0;
				$data ['ordersn'] = $ordersn;
				$data ['mv'] = $uprice ['MV'];
				$data ['jifeng'] = $uprice ['PV'];
				$data ['userid'] = $userid;
				$data ['bzvu'] = $bzvu;
				$uaid = $dao->add ( $data );
				if ($uaid) {
					return true;
				} else {
					return false;
				}
			} else {
				return false;
			}
		}
	}
	
	/**
	 * 充值流水
	 *@date 2013-12-11
	 *@time 下午2:50:10
	 *@author tb
	 */
	function assets(){
		$where = array();
		$dao = M("UserAssets");
		if($_REQUEST['platform']){
			$conditon['platform'] = $_REQUEST['platform'];
			$where['platform'] = $_REQUEST['platform'];
		}
		if($_REQUEST['changtype']){
			$conditon['changtype'] = $_REQUEST['changtype'];
			$where['changtype'] = $_REQUEST['changtype'];
		}
		if($_REQUEST['subject']){
			$conditon['subject'] = $_REQUEST['subject'];
			$where['subject'] = $_REQUEST['subject'];
		}

		if($_REQUEST['time']){ //传参是时间戳格式
			$starttime = $_REQUEST['starttime'];
			$endtime = $_REQUEST['endtime'];
			if($starttime) $conditon['starttime'] = date('Y-m-d',$starttime);
			if($endtime) $conditon['endtime'] = date('Y-m-d',$endtime);
		}else{//传参是日期格式
			$conditon['starttime'] = $_REQUEST['starttime'];
			$conditon['endtime'] = $_REQUEST['endtime'];
			$starttime = gettime($_REQUEST['starttime']." 00:00:00");
			$endtime = gettime($_REQUEST['endtime']." 23:59:59");
		}
		if($starttime && $endtime){
			$where['ctime'] = array('between',array($starttime,$endtime));
		}elseif($starttime){
			$where['ctime'] = array('EGT',$starttime);
		}elseif($endtime){
			$where['ctime'] = array('ELT',$endtime);
		}
		
		$where=parent::create_where($where);
		$count = $dao->where($where)->count();
		$this->assign("totalCount",$count);
		$page=new Page($count,$this->listrows);
		$offset = $page -> firstRow;
		$length = $page -> listRows;
		$list = $dao->field('*')->where($where)->order($this->order)->limit($offset,$length)->select();
		//$list = $this->money->assets('','SMW01','','','ADMIN_ADD,TENPAY_ADD');echo 44;
		//print_r($list);
		
		$sum = self::assets_sum($where['subject'],$where);
		
		$this->assign('menu',$_REQUEST['menu']);
		$this->assign('where',$conditon);
		$this->assign('sum',$sum);
		$this->assign('list',$list);
		$this->display();
	}
	
	/**
	 * 统计流水总金额
	 * 
	 * **/
	function assets_sum($subject='',$where = ''){
		$dao = M("UserAssets");
		if($subject){
			$sum[$subject] = round($dao->where($where)->sum('value/100'),2);
		}else{
			$where['subject'] = 'CV';
			$sum['CV'] = round($dao->where($where)->sum('value/100'),2);
			$where['subject'] = 'BV';
			$sum['BV'] = round($dao->where($where)->sum('value/100'),2);
			$where['subject'] = 'PV';
			$sum['PV'] = round($dao->where($where)->sum('value/100'),2);
			$where['subject'] = 'MV';
			$sum['PV'] = round($dao->where($where)->sum('value/100'),2);
		}
		return $sum;
	}
	
	
	
	/**
	 * 在线充值记录
	 *@date 2014-03-07
	 *@time 下午3:50:10
	 *@author tb
	 */
	function onlinepay(){
		$where = array();
		$dao = M("Onlinepay");
		
		$condition['retcode'] = $_REQUEST['retcode'];
		if($condition['retcode'] == 1){
			$where['retcode'] = 0;
		}elseif($condition['retcode'] == 2){
			$where['retcode'] = array('NEQ',0);
		}		
		if($_REQUEST['platform']){
			$condition['platform'] = $_REQUEST['platform'];
			$where['platform'] = $_REQUEST['platform'];
		}
	
		$condition['starttime'] = $_REQUEST['starttime'];
		$condition['endtime'] = $_REQUEST['endtime'];
		$starttime = gettime($condition['starttime']." 00:00:00");
		$endtime = gettime($condition['endtime']." 23:59:59");

		if($starttime && $endtime){
			$where['ctime'] = array('between',array($starttime,$endtime));
		}elseif($starttime){
			$where['ctime'] = array('EGT',$starttime);
		}elseif($endtime){
			$where['ctime'] = array('ELT',$endtime);
		}
	
		$where=parent::create_where($where);
		$count = $dao->where($where)->count();
		$this->assign("totalCount",$count);
		$page=new Page($count,$this->listrows);
		$offset = $page -> firstRow;
		$length = $page -> listRows;
		$list = $dao->field('*')->where($where)->order($this->order)->limit($offset,$length)->select();

		$this->assign('menu',$_REQUEST['menu']);
		$this->assign('condition',$condition);
		$this->assign('list',$list);
		$this->display();
	}
	
	/*
	 * 退款记录列表
	* */
	function refund(){
		$dao = M('UserRefund');
		$where = array();
		if($_REQUEST['moneytype']){
			$where['moneytype'] = $_REQUEST['moneytype'];
		}
		$where['element'] = array('in',$this->element);
		$refundtype=M("SystemConfig")->field('value,title')->where($where)->select();
		$refundtype = self::_arr2to1($refundtype,'value');
		$refundtype = implode(',', $refundtype);
		$where['refundtype'] = array('IN',$refundtype);
		$where = parent::create_where($where);
		$count = $dao->where($where)->count();
		if($count){
			$page=new Page($count,$this->listrows);
			$list = $dao->where($where)->order($this->order)->limit($page->firstRow.','.$page->listRows)->select();
			$this->assign('list',$list);
		}
		$this->assign("totalCount",$count);
		$this->assign('where',$where);
		$this->display();
	}
	
	function refundauth(){
		//判断权限
		$this->cw();
		
		if($_REQUEST['id']){
			$dao = M('UserRefund');
			//获取退款类型
			$where['element'] = array('in',$this->element);
			$refundtype=M("SystemConfig")->field('value,title')->where($where)->select();
			$info = $dao->find($_REQUEST['id']);
			$this->assign('info',$info);
			$this->assign('refundtype',$refundtype);
			$this->display();
		}else{
			$this->_ajm(300,'信息不存在！');
		}		
	}
	
	/**
	 * 个人财务信息查询
	 * @date 2013-10-15
	 * @time 下午2:50:10
	 */
	function personal() {
		// 个人财务信息查询
	} // end personal
}